﻿using Cofe.Core.Utils;
using Cofe.Core.Validater;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Security;
using System.Collections.Concurrent;

namespace Cofe.Core
{
    public static class DecoratorExtension
    {
        public static bool IsWebResource<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.IsWebResource(item);
        }

        public static bool IsWebProperty<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.IsWebProperty(item);
        }

        public static bool IsWebAction<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.IsWebAction(item);
        }

        public static bool IsAction<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.IsAction(item);
        }

        public static bool IsAction<T>(T item, out ActionAttribute actionAttribute)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.IsAction(item, out actionAttribute);
        }

        public static bool HasAlias<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.HasAlias(item);
        }

        /// <summary>
        /// Return alias of the specified property, or property.ToString() if it's not specified.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string getAlias<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.getAlias(item);
        }

        public static PermissionType GetRequirePermissionForRead<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.GetRequirePermissionForRead(item);
        }

        public static PermissionType GetRequirePermissionForWrite<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.GetRequirePermissionForWrite(item);
        }

        /// <summary>
        /// Whether a property is tagged with MetadataAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool IsMetadata<T>(T item)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.IsWebMetadata(item);
        }

        public static bool HasDefaultValue(object property)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.HasDefaultValue(property);
        }

        public static object GetDefaultValue(object property)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.GetDefaultValue(property);
        }

        public static Type GetDefaultType(object property)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.GetDefaultType(property);
        }

        public static PropertyCacheOptions GetPropertyCacheOption(object property)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.GetPropertyCacheOption(property);
        }

        public static DatabaseCacheOptions GetDatabaseCacheOption(object property)
        {
            return CofeServices.PropertyDictionary.AttributeHelper.GetDatabaseCacheOption(property);
        }



        private static ConcurrentDictionary<MediaType, string> _mimeTypeCache = new ConcurrentDictionary<MediaType, string>();

        /// <summary>
        /// Return the MIMEType of a specific MediaType.
        /// </summary>
        /// <param name="mediaType"></param>
        /// <returns></returns>
        public static string GetMIMEType(MediaType mediaType)
        {
            if (!_mimeTypeCache.ContainsKey(mediaType))
                _mimeTypeCache[mediaType] = AttributeUtils<MIMETypeAttribute>.FindAttribute(mediaType).MIMEType;
            return _mimeTypeCache[mediaType];

        }
    }
}
